package com.project.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.blog.common.Result;
import com.project.blog.dto.PageInfo;
import com.project.blog.entity.Category;
import com.project.blog.entity.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.project.blog.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 栏目表 前端控制器
 * </p>
 *
 * @author 代码奔腾
 * @since 2022-12-30
 */
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {
    @GetMapping("/")
    public Result<?> list() {
        return Result.success(categoryService.list());
    }

    @PostMapping("/page")
    public Result<?> findPage(@RequestBody PageInfo pageInfo) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Category::getId);
        Page<Category> page = categoryService.page(new Page<>(
                        pageInfo.getPageNum(),
                        pageInfo.getPageSize()),
                wrapper
        );
        return Result.success(page);
    }

    @PostMapping("/save")
    public Object save(@Validated @RequestBody Category category) {
        categoryService.saveOrUpdate(category);
        return Result.success();
    }

    @PostMapping("/delBatch")
    public Object deleteBatch(@RequestBody List<Integer> ids) {
        categoryService.removeByIds(ids);
        return Result.success();
    }
}
